<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table :data="rentList" style="width: 100%" border>
            <el-table-column
              prop="index"
              label="序号"
              width="50"
              type="index"
            />
            <el-table-column prop="name" label="租赁楼宇" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button type="text">
                  <a target="_blank" :href="preview(row)">{{
                    row.contractName
                  }}</a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <el-button type="text"
                  ><a target="_blank" :href="row.contractUrl"
                    >合同下载</a
                  ></el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { getEnterpriseDetailAPI } from '@/api/enterprise'
export default {
  name: 'RentDetail',

  data () {
    // 知识点：常见的文件类型都有网站支持在线预览，只需要拼接好url即可，一般都是拼接文件的资源路径
    // a标签的src写文件资源路径，浏览器默认会识别出来并下载，无需前端做其他处理也行
    return {
      previewURL: 'https://view.officeapps.live.com/op/view.aspx?src=',
      previewPDFURL: 'http://view.xdocin.com/xdoc?_xdoc=',
      rentList: []
    }
  },
  methods: {
    preview (row) {
      let url
      if (row.contractName.includes('pdf')) {
        url = this.previewPDFURL + row.contractUrl
      } else {
        url = this.previewURL + row.contractUrl
      }
      return url
    },
    async getRents () {
      const { data } = await getEnterpriseDetailAPI(this.$route.params.id)
      this.rentList = data.rent
      console.log(data.rent)
    }
  },
  created () {
    this.getRents()
  }
}
</script>

<style scoped lang="scss">
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }
    .table-container {
      padding: 20px;
    }
    .preview {
      img {
        width: 100px;
      }
    }
  }
}
</style>
